package com.dudu.timer;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.dudu.dao.PushNoticeInfoDao;
import com.dudu.model.PushNoticeInfo;

import core.CoreDao;
import net.sf.json.JSONObject;
/**
 * 开始定时语音SESSION推送任务
 * 向调用方推送IMS返回的SESSION_id
 * @author gaoxs
 *
 */
public class SendVoiceInfoThread implements Runnable {

	private static final Logger log =  core.util.Logger.getLogger(SendVoiceInfoThread.class.getSimpleName());
	
	private CoreDao dao = null;
	private PushNoticeInfo mSendNotice = null;
	private PushNoticeInfoDao mPushNoticeInfoDao = null;
	
	
	public SendVoiceInfoThread(CoreDao dao,PushNoticeInfo sendNotice,PushNoticeInfoDao pushNoticeInfoDao){
		
		this.dao = dao;
		mSendNotice = sendNotice;
		mPushNoticeInfoDao = pushNoticeInfoDao;
		
	}
	
	@Override
	public void run() {
		//推送地址  
		String ret=SendVoiceUtil.sendVoiceInfo(mSendNotice);
		mSendNotice.setSend_count(mSendNotice.getSend_count()+1);
		mPushNoticeInfoDao.updateSendCount(dao, mSendNotice);
		
		String result = "";
		
		if(StringUtils.isNotBlank(ret)){			
			JSONObject retJson = JSONObject.fromObject(ret);
			 result = retJson.optString("result");		
		}
		
		if(result.equals("0")){
			
			mSendNotice.setStatus("0");
			int retSta = mPushNoticeInfoDao.updateStatus(dao, mSendNotice);
			if(retSta >0){
				log.info("通知SessionID 更新本地状态成功");
			}else{
				log.info("通知SessionID 更新本地状态成功");
			}
			
		}
		
		else{
			
			mSendNotice.setStatus("1");
			int retSta = mPushNoticeInfoDao.updateStatus(dao, mSendNotice);
			if(retSta >0){
				log.info("通知SessionID 更新本地状态成功");
			}else{
				log.info("通知SessionID 更新本地状态成功");
			}
			
		}
		
		mSendNotice.setRun_thread("");
		mPushNoticeInfoDao.updateRunThread(dao, mSendNotice);
		
	}
	
}
